/**
 * *****************************************************************
 * @file    adt3102_adc.h
 * @author  WuHao(hwu@andartechs.com.cn)
 * @version 1.0.0
 * @date    2020-11-09
 * @brief
 *
 *                 Copyright (c) 2020, Andar Technologies Inc.
 *                           www.andartechs.com 
 *
 * *****************************************************************
 */
#ifndef __ADC_H
#define __ADC_H
/*------------------------------ include -------------------------------------*/
#include "adt3102_type_define.h"


/*------------------------------ function ------------------------------------*/
/*
ADC0_FIFO : adc0 sample source data fifo address 
ADC1_FIFO : adc1 sample source data fifo address 
*/
#define ADC0_FIFO  0x40023000
#define ADC1_FIFO  0x40023004

/*
ADC_8P3M : config adc sample rate 8.3M 
ADC_16P6M : config adc sample rate 16.6M 
*/
#define ADC_8P3M   1
#define ADC_16P6M  0

/*
ADC0SEL : select adc0 channel sample 
ADC1SEL : select adc1 channel sample
ADC01SEL : select adc0 and adc1 channel sample
*/
#define ADC0SEL    0
#define ADC1SEL    1
#define ADC01SEL   2

/* GPADC select */
#define GPADC_AIO1    1     //temperature Compensation
#define GPADC_IN2     2     //VBE
#define GPADC_IN3     3   
#define GPADC_IN4     4     
#define GPADC_IN5     5     //powerdector 
#define GPADC_IN6     6     //VBG
#define BG_ATB_OUT    7   
#define VGA_ATB_OUT   8   
#define GPADC_IN18_1  9       
#define GPADC_IN18_2  10    //vtune    
#define GPADC_IN18_3  11  
#define ADC_ATB_OUT   12  


/*------------------------------ typedef -------------------------------------*/
/* FirDownsample : config fir filter coefficients */
typedef struct
{
    uint32 firReg0  ; 
    uint32 firReg1  ; 
    uint32 firReg2  ; 
    uint32 firReg3  ; 
    uint32 firReg4  ; 
    uint32 firReg5  ; 
    uint32 firReg6  ; 
    uint32 firReg7  ; 
    uint32 firReg8  ; 
    uint32 firReg9  ; 
    uint32 firReg10 ; 
    uint32 firReg11 ; 
    uint32 firReg12 ; 
    uint32 firReg13 ; 
    uint32 firReg14 ; 
    uint32 firReg15 ;
    uint32 filter_coefficient;
    
}FirDownsample;


/*------------------------------ function ------------------------------------*/
/**
 * @brief   adt3102 adc setting.
 * @param   rx0En: RX channel enable.
 * @param   rx1En: RX channel enable.
 * @param   wnSel: 1:windowing cof select 0 hanning 1 hamming.
 * @param   adcClk: ADC_8P3M/ADC_16P6M.
 * @param   chirpNumber: chirp number.
 * @return  None.
 * @note
 */
void adt3102AdcSetting(int32 rx0En,int32 rx1En,int32 wnSel,int32 adcClk,uint32 chirpNumber);

/**
 * @brief   Generate adc data with object information.
 * @param   step     :siggen_step[31:16] :step.
 * @param   chgStep  :siggen_step[15:0] :chg_stp.
 * @param   comp     :siggen_ctrl[17] :comp.
 * @param   ang      :siggen_ctrl[15:0]:ang.
 * @return  None.
 * @note
 */
void siggen(uint16 step, uint16 chgStep, uint16 comp, uint16 siggenEn, uint16 ang);

/**
 * @brief   baseBand Bias Setting, enable signal for ztc bias for BB.
 * @param   bbBiasEn: enable signal for ztc bias for BB..
 * @return  None.
 * @note
 */
void baseBandBiasSetting(int32 bbbiasEn);

/**
 * @brief   enable adc, and enable clk output to adc.
 * @param   adc0Enable~adc1Enable: adc0 enable~adc1 enable.
 * @return  None.
 * @note
 */
void adcEnable(int32 adc0Enable,int32 adc1Enable);

/**
 * @brief   adc sample init, ch0 enable and ch1 enable.
 * @param   ch0~ch1: .
 * @return  None.
 * @note
 */
void adcSampleInit(int32 ch0, int32 ch1);

/**
 * @brief   bbadc with windo.
 * @param   cofSel : windowing cof select 0 hanning 1 hamming.
 * @param   bypass : bypass:2: set to 1 to bypasse windowing.
 * @param   wnLen  : window len, valid settings: 32,64,128,256,512.
 * @return  None.
 * @note
 */
void adcWnpara(int32 cofSel, int32 bypass, int32 wnLen);

/**
 * @brief   the function is used to adc fir bypass and dowmsample.
 * @param   firBypass : fir byass.
 * @param   downSample : set adc sample dowmsample.
 * @return  None.
 * @note
 */
void adcFirDownsample(int32 firBypass, uint16 downSample);

/**
 * @brief   adc sample config, is used to extract data.
 * @param   intervalNum: pll adc interval num.
 * @param   endNum: the interval_num = start_num + chirp_sample*2 + end_num, which
 *                     means head, valid and tail separately.
 * @param   rangeFftPoint: a chirp include adc point number.
 * @param   downSample: adc downSample.
 * @param   chirpNum: fmcw chirp number.
 * @return  None.
 * @note
 */
void dataSampleConfig(uint16 intervalNum,uint16 endNum,uint16 rangeFftPoint,uint16 downSample, uint16 chirpNum);

/**
 * @brief   adc sampling,DMA moves the data to the specified address,where the ADC0
 *          source address is ADC0_FIFO,the ADC0 destination address is 0x20008000,
 *          where the ADC1 source address is ADC1_FIFO,the ADC1 destination address
 *          is 0x20008000+adc0 data space;The sampling time of adc is synchronized with
 *          the time of sending chirp wave.
 * 
 *  srcAddr |11 12 13 14 15...............................................1(adcSamples) |
 *          |21 22 23 24 25...............................................2(adcSamples) |
 *          |31 32 33 34 35...............................................3(adcSamples) |
 *          |...........................................................................|
 *          |...........................................................................|
 *          |...........................................................................|
 *          |...........................................................................|
 *          |...........................................................................|
 *          |(chirpNumber)1 (chirpNumber)2 (chirpNumber)3......(chirpNumber)(adcSamples)|
 * @param   adcSamples: ADC sampling points.
 * @param   chirpNumber :Fmcw chirp number.
 * @param   dstAddr     :Destination address.
 * @return  None.
 * @note
 */
void adcSampleStart(
    uint16 adcSel, 
    uint32 adcSamples, 
    uint32 chirpNumber, 
    uint32 ch0srcAddr,
    uint32 ch0dstAddr,
    uint32 ch1srcAddr,
    uint32 ch1dstAddr);

/**
 * @brief   when FMCW trigger signal as ADC start signal, the funtion is start signal.
 * @param   None.
 * @return  None.
 * @note
 */
void adcFmcwStart(void);

/**
 * @brief   about power.
 * @param   enableSigInput:.
 * @return  None
 * @note
 */
void gpadcEnable(uint16 enableSigInput);

/**
 * @brief   adc bias setting.
 * @param   adc0Bias0~adc0Bias5:
 * @param   adc1Bias0~adc1Bias5:
 * @return  None.
 * @note
 */
void adcBiasSetting(int32 adc0Bias0, int32 adc0Bias1, int32 adc0Bias2, int32 adc0Bias3,
                    int32 adc0Bias4, int32 adc0Bias5, int32 adc1Bias0, int32 adc1Bias1,
                    int32 adc1Bias2, int32 adc1Bias3, int32 adc1Bias4, int32 adc1Bias5);

/**
 * @brief   adc clock switch.
 * @param   adcClk: ADC_8P3M/ADC_16P6M.
 * @return  None.
 * @note
 */
void adcClkSwitch(int32 adcClk);    

/**
 * @brief   Get number of intervals.
 * @param   t0~t2 : .
 * @return  None.
 * @note
 */
uint32 getIntervalNum(uint32 t0,uint32 t1,uint32 t2);

/**
 * @brief   correction of ADC sampling problem.
 *          Matlab code: if((floor(data_tmp/64)>31) | (floor(data_tmp/64)<31))
 *                          data_tmp = floor(data_tmp/64)64+mod(data_tmp,64)1.75;
 *                          end
 * @param   addr: Destination address.
 * @param   adcSamples: ADC sampling points.
 * @param   chirpNum: chirp number.
 * @return  None.
 * @note
 */
void adcDataCorrection(uint32 addr,uint16 adcSamples,uint16 chirpNum);

/**
 * @brief   read AIO value.
 * @return  uint8 : GPADC value for AIO, reference voltage is 1.35V.
 * @note
 */
uint8 gpadcReadAIO(void);

/**
 * @brief   adc bypass testing sw enable.
 * @param   None.
 * @return  None.
 * @note
 */
void adt3102AdcTest(void);
#endif




